package demo01;

import java.util.List;
import java.util.Random;

public class MyThread extends Thread{
	private  List<Integer> prizes;
	private  Box box1;
	private  Box box2;
	private  Random random = new Random();


	public MyThread(List<Integer> prizes, Box box1, Box box2) {
		this.prizes = prizes;
		this.box1 = box1;
		this.box2 = box2;
	}
	public void run() {
		while (true) {
			Integer prize;
			synchronized (prizes) {
				if (prizes.isEmpty()) {
					break;
				}
				int index = random.nextInt(prizes.size());
				prize = prizes.remove(index);
			}

			if (random.nextBoolean()) {
				box1.addPrize(prize);
			} else {
				box2.addPrize(prize);
			}

			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
				break;
			}
		}
	}
}
